package com.ybwh.springboot2.common.auth.satoken;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.ybwh.springboot2.common.annotation.LoginRequired;
import com.ybwh.springboot2.common.auth.LoginRequiredInterceptor;
import com.ybwh.springboot2.common.auth.LoginService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * 内部接口的拦截器
 *
 * @author: Fan Beibei
 * @date: 2018/5/4 17:56
 * @Modified By:
 */
@Slf4j
@AllArgsConstructor
public class SaTokenHandlerInterceptor extends LoginRequiredInterceptor {

    private LoginService loginService;

    @Override
    protected Long getUserId(HttpServletRequest request) {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        Long userId = (Long) tokenInfo.getLoginId();
        return userId;
    }

    @Override
    protected boolean validLoginRequired(HttpServletRequest request, HttpServletResponse response, LoginRequired loginRequired) {
        SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
        return null != tokenInfo;
    }


    @Override
    protected boolean validFuncAccess(String funcCode, Long userId) {

        Set<String> funcList = loginService.getUserFuncList(userId);

        if(null == funcList || 0 == funcList.size()){
            return false;
        }

        if(funcList.contains(funcCode)){
            return true;
        }

        return false;
    }
}
